package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.Comment;
import com.zdd.ehe.entity.Post;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.entity.User;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.ICommentService;
import com.zdd.ehe.service.IPostService;
import com.zdd.ehe.service.IUserService;
import com.zdd.ehe.vo.CommentVo;
import com.zdd.ehe.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-18
 */
@Api(tags = "评论模块")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private ICommentService commentService;
    @Resource
    private IUserService userService;
    @Resource
    private IPostService postService;

    //发布
    @ApiOperation("发布评论接口")
    @PostMapping("/publish")
    public Result publish(@RequestBody Comment comment){
        if(comment.getContent() == null){
            return Result.error(4001,"评论内容为空");
        }
        if(comment.getUserId() == null || comment.getCommentId() == null && comment.getPostId() == null){
            return Result.error(4002,"参数缺失");
        }
        User userx = userService.getById(comment.getUserId());
        if(userx == null){
            return Result.error(4003,"用戶编号错误");
        }
        if(comment.getCommentId() == null){
            Post postx = postService.getById(comment.getPostId());
            if(postx == null){
                return Result.error(4004,"帖子编号错误");
            }else{
                if(postx.getDel().equals("0")){
                    return Result.error(4005,"该帖子已被封禁");
                }
            }
        }else{
            Comment commentx = commentService.getById(comment.getCommentId());
            if(commentx == null){
                return Result.error(4006,"评论编号错误");
            }
        }
        comment.setDel("1");
        comment.setLikes(0);
        int result = commentService.insrt(comment);
        if(result != 1){
            return Result.error(4003,"数据保存失败");
        }
        return Result.success("发布成功");
    }


    @ApiOperation("根据帖子ID获取对应评论列表接口")
    @PostMapping("/getcommentbypostid")
    public Result getcommentbypostid(@RequestParam("id") Integer id){
        if(id == null){
            return Result.error(4001,"参数缺失");
        }
        Post postx = postService.getById(id);
        if(postx == null){
            return Result.error(4002,"帖子编号错误");
        }
        if(postx.getDel().equals("0")){
            return Result.error(4003,"该帖子已被封禁");
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", id);
        List<Comment> commentList = commentService.list(queryWrapper);
        if(commentList.size() == 0){
            return Result.error(4004,"无数据");
        }

        List<CommentVo> commentVoList = new ArrayList<>();
        for(Comment comment:commentList){
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(comment,commentVo);
            User user = userService.getById(comment.getUserId());
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            commentVo.setUser(userVo);
            commentVoList.add(commentVo);
        }

        return Result.success("查询成功", commentVoList);
    }


    @ApiOperation("分页获取评论接口")
    @PostMapping("/getallcommentbypage")
    public Result getAllCommentByPage(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        Page<Comment> pagex = new Page<>(page,pageSize);
        Page<Comment> pageResult = commentService.getAllCommentByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }

}
